//
//  WLUseManager.m
//  WanLian
//
//  Created by muzhuang on 2018/9/20.
//  Copyright © 2018年 wanlian. All rights reserved.
//

#import "WLUseManager.h"
#import "JPUSHService.h"
@interface WLUseManager ()
@end
@implementation WLUseManager

#pragma mark-初始化工具类
+ (instancetype)shareUser
{
    static WLUseManager *userManager = nil;
    static dispatch_once_t once;
    dispatch_once(&once, ^{
        userManager = [[WLUseManager alloc]init];
    });
    return userManager;
}
- (void)requestRouterInfo {
    if (self.routerModel) {
        self.routerModel = nil;
    }
    WLWkSelf
    [WLHttpManager WLRequestMethod:RequestTypePost urlStr:@"device/get/currentdevice" withParamsDic:nil success:^(NSDictionary *successDict) {
        if (!successDict[@"object"] || [successDict[@"object"] isKindOfClass:[NSNull class]]) {
            return ;
        }
        WLRoutersModel *routerModel = [WLRoutersModel mj_objectWithKeyValues:successDict[@"object"]];
        [weakSelf configTouterManagerWithModel:routerModel];
        [[NSNotificationCenter defaultCenter] postNotificationName:kLoadedRouterInfo object:nil];
    } failure:^(NSString *errorMsg, NSInteger errorTag) {
        if (errorTag != LoginExpireTag) {
            [weakSelf requestRouterInfo];
        }
    } error:^(NSError *error) {
        if (error != nil) {
            [weakSelf requestRouterInfo];
        }
    } inView:nil];
    
}
- (void)configTouterManagerWithModel:(WLRoutersModel *)routerModel {
    if (routerModel.macAddress != nil) {
        _routerModel = routerModel;
    }
}
- (NSString *)userInfoPath
{
    NSString *docPath=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
    NSString *path=[docPath stringByAppendingPathComponent:@"userInfo.data"];
    return path;
}

- (void)updateUserValue:(WLUserInfo *)user
{
    if (user == nil) {
        return;
    }
    NSString *path = [self userInfoPath];
    [self removeUserInfoPath:path];
    
    NSString *userTag = user.userTag;
    if (userTag.length > 0) {
        [JPUSHService setAlias:userTag completion:nil seq:0];
    }
    [NSKeyedArchiver archiveRootObject:user toFile:path];
}

- (WLUserInfo *)getUserInformation
{
    NSString *path = [self userInfoPath];
    WLUserInfo *user = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
    return user;
}

- (NSString *)getUserRouterTag
{
    NSString *path = [self userInfoPath];
    WLUserInfo *user = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
    if (user.userTag==nil) {
        return @"";
    }
    return user.userTag;
}

- (NSString *)getUserCurrentRouter
{
    NSString *path = [self userInfoPath];
    WLUserInfo *user = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
    if (user.devicekey==nil) {
        return @"";
    }
    return user.devicekey;
}

- (void)removeUserInfoPath:(NSString *)userPath
{
    if ([[NSFileManager defaultManager] fileExistsAtPath:userPath]) {
        [[NSFileManager defaultManager] removeItemAtPath:userPath error:nil];
    }
}

- (void)clearUserInfo
{
    NSString *path = [self userInfoPath];
    [self removeUserInfoPath:path];
    self.routerModel = nil;
}

- (BOOL)isLogin
{
//    if ([self getUserInformation] != nil) {
//        return YES;
//    }
    if ([[NSUserDefaults standardUserDefaults] objectForKey:USER_SESSION] != nil) {
        return YES;
    }
    return NO;
}


@end
